#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask_restful import Resource
from flask import make_response
from utility.odoo_manager import OdooManager
import base64
import logging

FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)


class ImageAPI(Resource):
    """
    将模型的一条记录的image字段返回成url
    :request: get
    :param model_name: 模型名称
    :param record_id: 记录id
    :param field_name: Binary格式字段名
    :returns: json string
    """

    def get(self, model_name, record_id, field_name):
        odoo = OdooManager().get_instance()
        record_dict = odoo.env[model_name].browse(record_id).read([field_name])[0]
        response = make_response(base64.urlsafe_b64decode(record_dict[field_name]))
        response.content_type = "image/jpeg"
        return response
